import {
  FieldMetadataType,
  NumberDataType,
  RelationType,
} from 'twenty-shared/types';

import { type FlatFieldMetadata } from 'src/engine/metadata-modules/flat-field-metadata/types/flat-field-metadata.type';
import { type AllStandardObjectFieldName } from 'src/engine/workspace-manager/twenty-standard-application/types/all-standard-object-field-name.type';
import { createStandardFieldFlatMetadata } from 'src/engine/workspace-manager/twenty-standard-application/utils/create-standard-field-flat-metadata.util';
import { createStandardRelationFieldFlatMetadata } from 'src/engine/workspace-manager/twenty-standard-application/utils/create-standard-relation-field-flat-metadata.util';
import { type StandardFieldMetadataIdByObjectAndFieldName } from 'src/engine/workspace-manager/twenty-standard-application/utils/get-standard-field-metadata-id-by-object-and-field-name.util';

export const buildWorkspaceMemberStandardFlatFieldMetadatas = ({
  createdAt,
  workspaceId,
  standardFieldMetadataIdByObjectAndFieldName,
}: {
  createdAt: Date;
  workspaceId: string;
  standardFieldMetadataIdByObjectAndFieldName: StandardFieldMetadataIdByObjectAndFieldName;
}): Record<
  AllStandardObjectFieldName<'workspaceMember'>,
  FlatFieldMetadata
> => ({
  id: createStandardFieldFlatMetadata({
    objectName: 'workspaceMember',
    workspaceId,
    options: {
      fieldName: 'id',
      type: FieldMetadataType.UUID,
      label: 'Id',
      description: 'Id',
      icon: 'Icon123',
      isSystem: true,
      isNullable: false,
      isUIReadOnly: true,
      defaultValue: 'uuid',
      createdAt,
    },
    standardFieldMetadataIdByObjectAndFieldName,
  }),
  createdAt: createStandardFieldFlatMetadata({
    objectName: 'workspaceMember',
    workspaceId,
    options: {
      fieldName: 'createdAt',
      type: FieldMetadataType.DATE_TIME,
      label: 'Creation date',
      description: 'Creation date',
      icon: 'IconCalendar',
      isNullable: false,
      isUIReadOnly: true,
      defaultValue: 'now',
      settings: { displayFormat: 'RELATIVE' },
      createdAt,
    },
    standardFieldMetadataIdByObjectAndFieldName,
  }),
  updatedAt: createStandardFieldFlatMetadata({
    objectName: 'workspaceMember',
    workspaceId,
    options: {
      fieldName: 'updatedAt',
      type: FieldMetadataType.DATE_TIME,
      label: 'Last update',
      description: 'Last time the record was changed',
      icon: 'IconCalendarClock',
      isNullable: false,
      isUIReadOnly: true,
      defaultValue: 'now',
      settings: { displayFormat: 'RELATIVE' },
      createdAt,
    },
    standardFieldMetadataIdByObjectAndFieldName,
  }),
  deletedAt: createStandardFieldFlatMetadata({
    objectName: 'workspaceMember',
    workspaceId,
    options: {
      fieldName: 'deletedAt',
      type: FieldMetadataType.DATE_TIME,
      label: 'Deleted at',
      description: 'Date when the record was deleted',
      icon: 'IconCalendarMinus',
      isNullable: true,
      isUIReadOnly: true,
      settings: { displayFormat: 'RELATIVE' },
      createdAt,
    },
    standardFieldMetadataIdByObjectAndFieldName,
  }),
  position: createStandardFieldFlatMetadata({
    objectName: 'workspaceMember',
    workspaceId,
    options: {
      fieldName: 'position',
      type: FieldMetadataType.POSITION,
      label: 'Position',
      description: 'Workspace member position',
      icon: 'IconHierarchy2',
      isSystem: true,
      isNullable: false,
      defaultValue: 0,
      createdAt,
    },
    standardFieldMetadataIdByObjectAndFieldName,
  }),
  name: createStandardFieldFlatMetadata({
    objectName: 'workspaceMember',
    workspaceId,
    options: {
      fieldName: 'name',
      type: FieldMetadataType.FULL_NAME,
      label: 'Name',
      description: 'Workspace member name',
      icon: 'IconCircleUser',
      isNullable: false,
      createdAt,
    },
    standardFieldMetadataIdByObjectAndFieldName,
  }),
  colorScheme: createStandardFieldFlatMetadata({
    objectName: 'workspaceMember',
    workspaceId,
    options: {
      fieldName: 'colorScheme',
      type: FieldMetadataType.TEXT,
      label: 'Color Scheme',
      description: 'Preferred color scheme',
      icon: 'IconColorSwatch',
      isSystem: true,
      isNullable: false,
      defaultValue: "'System'",
      createdAt,
    },
    standardFieldMetadataIdByObjectAndFieldName,
  }),
  locale: createStandardFieldFlatMetadata({
    objectName: 'workspaceMember',
    workspaceId,
    options: {
      fieldName: 'locale',
      type: FieldMetadataType.TEXT,
      label: 'Language',
      description: 'Preferred language',
      icon: 'IconLanguage',
      isSystem: true,
      isNullable: false,
      defaultValue: "'en'",
      createdAt,
    },
    standardFieldMetadataIdByObjectAndFieldName,
  }),
  avatarUrl: createStandardFieldFlatMetadata({
    objectName: 'workspaceMember',
    workspaceId,
    options: {
      fieldName: 'avatarUrl',
      type: FieldMetadataType.TEXT,
      label: 'Avatar Url',
      description: 'Workspace member avatar',
      icon: 'IconFileUpload',
      isSystem: true,
      isNullable: true,
      createdAt,
    },
    standardFieldMetadataIdByObjectAndFieldName,
  }),
  userEmail: createStandardFieldFlatMetadata({
    objectName: 'workspaceMember',
    workspaceId,
    options: {
      fieldName: 'userEmail',
      type: FieldMetadataType.TEXT,
      label: 'User Email',
      description: 'Related user email address',
      icon: 'IconMail',
      isSystem: true,
      isNullable: true,
      isUnique: true,
      createdAt,
    },
    standardFieldMetadataIdByObjectAndFieldName,
  }),
  calendarStartDay: createStandardFieldFlatMetadata({
    objectName: 'workspaceMember',
    workspaceId,
    options: {
      fieldName: 'calendarStartDay',
      type: FieldMetadataType.NUMBER,
      label: 'Start of the week',
      description: "User's preferred start day of the week",
      icon: 'IconCalendar',
      isSystem: true,
      isNullable: false,
      defaultValue: 7,
      settings: {
        dataType: NumberDataType.INT,
      },
      createdAt,
    },
    standardFieldMetadataIdByObjectAndFieldName,
  }),
  userId: createStandardFieldFlatMetadata({
    objectName: 'workspaceMember',
    workspaceId,
    options: {
      fieldName: 'userId',
      type: FieldMetadataType.UUID,
      label: 'User Id',
      description: 'Associated User Id',
      icon: 'IconCircleUsers',
      isSystem: true,
      isNullable: false,
      createdAt,
    },
    standardFieldMetadataIdByObjectAndFieldName,
  }),
  timeZone: createStandardFieldFlatMetadata({
    objectName: 'workspaceMember',
    workspaceId,
    options: {
      fieldName: 'timeZone',
      type: FieldMetadataType.TEXT,
      label: 'Time zone',
      description: 'User time zone',
      icon: 'IconTimezone',
      isSystem: true,
      isNullable: false,
      defaultValue: "'system'",
      createdAt,
    },
    standardFieldMetadataIdByObjectAndFieldName,
  }),
  dateFormat: createStandardFieldFlatMetadata({
    objectName: 'workspaceMember',
    workspaceId,
    options: {
      fieldName: 'dateFormat',
      type: FieldMetadataType.SELECT,
      label: 'Date format',
      description: "User's preferred date format",
      icon: 'IconCalendarEvent',
      isSystem: true,
      isNullable: false,
      defaultValue: "'SYSTEM'",
      options: [
        { value: 'SYSTEM', label: 'System', position: 0, color: 'turquoise' },
        {
          value: 'MONTH_FIRST',
          label: 'Month First',
          position: 1,
          color: 'red',
        },
        {
          value: 'DAY_FIRST',
          label: 'Day First',
          position: 2,
          color: 'purple',
        },
        { value: 'YEAR_FIRST', label: 'Year First', position: 3, color: 'sky' },
      ],
      createdAt,
    },
    standardFieldMetadataIdByObjectAndFieldName,
  }),
  timeFormat: createStandardFieldFlatMetadata({
    objectName: 'workspaceMember',
    workspaceId,
    options: {
      fieldName: 'timeFormat',
      type: FieldMetadataType.SELECT,
      label: 'Time format',
      description: "User's preferred time format",
      icon: 'IconClock2',
      isSystem: true,
      isNullable: false,
      defaultValue: "'SYSTEM'",
      options: [
        { value: 'SYSTEM', label: 'System', position: 0, color: 'sky' },
        { value: 'HOUR_24', label: '24HRS', position: 1, color: 'red' },
        { value: 'HOUR_12', label: '12HRS', position: 2, color: 'purple' },
      ],
      createdAt,
    },
    standardFieldMetadataIdByObjectAndFieldName,
  }),
  numberFormat: createStandardFieldFlatMetadata({
    objectName: 'workspaceMember',
    workspaceId,
    options: {
      fieldName: 'numberFormat',
      type: FieldMetadataType.SELECT,
      label: 'Number format',
      description: "User's preferred number format",
      icon: 'IconNumbers',
      isSystem: true,
      isNullable: false,
      defaultValue: "'SYSTEM'",
      options: [
        { value: 'SYSTEM', label: 'System', position: 0, color: 'turquoise' },
        {
          value: 'COMMAS_AND_DOT',
          label: 'Commas and dot (1,234.56)',
          position: 1,
          color: 'blue',
        },
        {
          value: 'SPACES_AND_COMMA',
          label: 'Spaces and comma (1 234,56)',
          position: 2,
          color: 'green',
        },
        {
          value: 'DOTS_AND_COMMA',
          label: 'Dots and comma (1.234,56)',
          position: 3,
          color: 'orange',
        },
        {
          value: 'APOSTROPHE_AND_DOT',
          label: "Apostrophe and dot (1'234.56)",
          position: 4,
          color: 'purple',
        },
      ],
      createdAt,
    },
    standardFieldMetadataIdByObjectAndFieldName,
  }),
  searchVector: createStandardFieldFlatMetadata({
    objectName: 'workspaceMember',
    workspaceId,
    options: {
      fieldName: 'searchVector',
      type: FieldMetadataType.TS_VECTOR,
      label: 'Search vector',
      description: 'Field used for full-text search',
      icon: 'IconUser',
      isSystem: true,
      isNullable: true,
      createdAt,
    },
    standardFieldMetadataIdByObjectAndFieldName,
  }),
  assignedTasks: createStandardRelationFieldFlatMetadata({
    objectName: 'workspaceMember',
    workspaceId,
    options: {
      fieldName: 'assignedTasks',
      label: 'Assigned tasks',
      description: 'Tasks assigned to the workspace member',
      icon: 'IconCheckbox',
      isNullable: false,
      createdAt,
      targetObjectName: 'task',
      targetFieldName: 'assignee',
      settings: {
        relationType: RelationType.ONE_TO_MANY,
      },
    },
    standardFieldMetadataIdByObjectAndFieldName,
  }),
  favorites: createStandardRelationFieldFlatMetadata({
    objectName: 'workspaceMember',
    workspaceId,
    options: {
      fieldName: 'favorites',
      label: 'Favorites',
      description: 'Favorites linked to the workspace member',
      icon: 'IconHeart',
      isNullable: false,
      createdAt,
      targetObjectName: 'favorite',
      targetFieldName: 'forWorkspaceMember',
      settings: {
        relationType: RelationType.ONE_TO_MANY,
      },
    },
    standardFieldMetadataIdByObjectAndFieldName,
  }),
  accountOwnerForCompanies: createStandardRelationFieldFlatMetadata({
    objectName: 'workspaceMember',
    workspaceId,
    options: {
      fieldName: 'accountOwnerForCompanies',
      label: 'Account Owner For Companies',
      description: 'Account owner for companies',
      icon: 'IconBriefcase',
      isNullable: false,
      createdAt,
      targetObjectName: 'company',
      targetFieldName: 'accountOwner',
      settings: {
        relationType: RelationType.ONE_TO_MANY,
      },
    },
    standardFieldMetadataIdByObjectAndFieldName,
  }),
  authoredAttachments: createStandardRelationFieldFlatMetadata({
    objectName: 'workspaceMember',
    workspaceId,
    options: {
      fieldName: 'authoredAttachments',
      label: 'Authored attachments',
      description: 'Attachments created by the workspace member',
      icon: 'IconFileImport',
      isSystem: true,
      isNullable: false,
      createdAt,
      targetObjectName: 'attachment',
      targetFieldName: 'author',
      settings: {
        relationType: RelationType.ONE_TO_MANY,
      },
    },
    standardFieldMetadataIdByObjectAndFieldName,
  }),
  connectedAccounts: createStandardRelationFieldFlatMetadata({
    objectName: 'workspaceMember',
    workspaceId,
    options: {
      fieldName: 'connectedAccounts',
      label: 'Connected accounts',
      description: 'Connected accounts',
      icon: 'IconAt',
      isNullable: false,
      createdAt,
      targetObjectName: 'connectedAccount',
      targetFieldName: 'accountOwner',
      settings: {
        relationType: RelationType.ONE_TO_MANY,
      },
    },
    standardFieldMetadataIdByObjectAndFieldName,
  }),
  messageParticipants: createStandardRelationFieldFlatMetadata({
    objectName: 'workspaceMember',
    workspaceId,
    options: {
      fieldName: 'messageParticipants',
      label: 'Message Participants',
      description: 'Message Participants',
      icon: 'IconUserCircle',
      isNullable: false,
      createdAt,
      targetObjectName: 'messageParticipant',
      targetFieldName: 'workspaceMember',
      settings: {
        relationType: RelationType.ONE_TO_MANY,
      },
    },
    standardFieldMetadataIdByObjectAndFieldName,
  }),
  blocklist: createStandardRelationFieldFlatMetadata({
    objectName: 'workspaceMember',
    workspaceId,
    options: {
      fieldName: 'blocklist',
      label: 'Blocklist',
      description: 'Blocklisted handles',
      icon: 'IconForbid2',
      isNullable: false,
      createdAt,
      targetObjectName: 'blocklist',
      targetFieldName: 'workspaceMember',
      settings: {
        relationType: RelationType.ONE_TO_MANY,
      },
    },
    standardFieldMetadataIdByObjectAndFieldName,
  }),
  calendarEventParticipants: createStandardRelationFieldFlatMetadata({
    objectName: 'workspaceMember',
    workspaceId,
    options: {
      fieldName: 'calendarEventParticipants',
      label: 'Calendar Event Participants',
      description: 'Calendar Event Participants',
      icon: 'IconCalendar',
      isNullable: false,
      createdAt,
      targetObjectName: 'calendarEventParticipant',
      targetFieldName: 'workspaceMember',
      settings: {
        relationType: RelationType.ONE_TO_MANY,
      },
    },
    standardFieldMetadataIdByObjectAndFieldName,
  }),
  timelineActivities: createStandardRelationFieldFlatMetadata({
    objectName: 'workspaceMember',
    workspaceId,
    options: {
      fieldName: 'timelineActivities',
      label: 'Events',
      description: 'Events linked to the workspace member',
      icon: 'IconTimelineEvent',
      isSystem: true,
      isNullable: true,
      createdAt,
      targetObjectName: 'timelineActivity',
      targetFieldName: 'workspaceMember',
      settings: {
        relationType: RelationType.ONE_TO_MANY,
      },
    },
    standardFieldMetadataIdByObjectAndFieldName,
  }),
});
